package com.bsb.biz.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.common.util.BeanUtils;
import com.bsb.base.common.util.DateUtils;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbCard;
import com.bsb.base.model.BsbCardExample;
import com.bsb.base.model.BsbCardExample.Criteria;
import com.bsb.base.model.BsbFirm;
import com.bsb.base.model.BsbUserExample;
import com.bsb.base.model.Card;
import com.bsb.biz.service.BsbCardService;
import com.bsb.biz.service.BsbFirmService;
import com.bsb.biz.service.BsbUserService;
import com.bsb.biz.shiro.UserUtils;

@Controller
@RequestMapping("/bsbCard")
public class BsbCardController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbCard/list.do";
	
	@Resource
	private BsbCardService bsbCardService;
	@Resource
	private BsbFirmService bsbFirmService;
	@Resource
	private BsbUserService bsbUserService;
	
	@RequiresPermissions("bsbCard:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,@RequestParam Map<String, Object> params,
			RedirectAttributes redirectAttributes,HttpServletRequest request,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		String cardNo = (String) params.get("cardNo");
		String firmId = (String) params.get("firmId");
		String name = (String) params.get("name");
		String status = (String) params.get("status");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		BsbCardExample bsbCardExample = new BsbCardExample();
		Criteria criteria = bsbCardExample.createCriteria();
		if(StringUtils.isNotEmpty(cardNo)){
			criteria.andIdEqualTo(Long.valueOf(cardNo));
		}
		if(StringUtils.isNotEmpty(firmId)){
			Long firm;
			try {
				firm = Long.parseLong(firmId);
				if(null != firm){
					criteria.andFirmIdEqualTo(firm);
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
				String queryString = request.getQueryString();
				redirectAttributes.addFlashAttribute("msg", "事务所ID必须为数字");
				return redirect+ "?" +queryString;
			}
		}
		BsbUserExample userExample = new BsbUserExample();
		if(StringUtils.isNotEmpty(name)){
			com.bsb.base.model.BsbUserExample.Criteria userCriteria = userExample.createCriteria();
			userCriteria.andNameLike("%"+name+"%");
		}
		if(StringUtils.isNotEmpty(status)){
			criteria.andStatusEqualTo(status);
		}
		if(null != startDate){
			criteria.andActiveDateGreaterThanOrEqualTo(startDate);
		}
		if(null != endDate){
			endDate = DateUtils.addDays(endDate, 1);
			criteria.andActiveDateLessThanOrEqualTo(endDate);
		}
		Long fid = UserUtils.currentUser().getFirmId();
		if(null == fid){
			model.addAttribute("msg", "账号异常");
			return "/BsbCard/list";
		}
		if(10000 != fid){
			criteria.andFirmIdEqualTo(fid);
		}
		int totalCount = bsbCardService.countCard(bsbCardExample,userExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbCardExample.setPageIndex((pageNum - 1) * pageSize);
		bsbCardExample.setPageSize(pageSize);
		bsbCardExample.setOrderByClause(" bsb_card.id DESC, activeDate  DESC");
		List<BsbCard> list = bsbCardService.getPageCardLike(bsbCardExample,userExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("cardNo", cardNo);
		model.addAttribute("firmId", firmId);
		model.addAttribute("name", name);
		model.addAttribute("status", status);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("bsbCards", list);
		return "/BsbCard/list";
	}
	
	@RequiresPermissions("bsbCard:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/BsbCard/add";
	}

	@RequiresPermissions("bsbCard:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbCard") BsbCard bsbCard) {
		bsbCardService.addBsbCard(bsbCard);
		return redirect;
	}
	
	@RequiresPermissions("bsbCard:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String del(Model model,
			@PathVariable("id") Long id) {
		bsbCardService.delBsbCardById(id);
		return redirect;
	}
	
	@RequiresPermissions("bsbCard:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toEdit(Model model,
			@PathVariable("id") Long id) {
		BsbCard bsbCard = bsbCardService.getBsbCardById(id);
		model.addAttribute("bsbCard", bsbCard);
		return "/BsbCard/edit";
	}

	@RequiresPermissions("bsbCard:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model, @ModelAttribute("bsbCard") BsbCard bsbCard) {
		bsbCardService.updateBsbCard(bsbCard);
		return redirect;
	}
	
	@RequiresPermissions("bsbCard:view")
	@RequestMapping(value = "/{id}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("id") Long id) {
		BsbCard bsbCard = bsbCardService.getBsbCardById(id);
		if(null != bsbCard){
			String userId = bsbCard.getUserId();
			if(StringUtils.isNotEmpty(userId)){
				bsbCard.setUsername(bsbUserService.getUserNameById(userId));
			}
		}
		model.addAttribute("bsbCard", bsbCard);
		return "/BsbCard/view";
	}
}
